﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FreedomERP.Models;
using System.Collections;
using System.Data.Objects;
using System.Text;
using FreedomERP.Comm.Utils;

namespace FreedomERP.Dao.Inv.Impl
{
    /// <summary>
    /// 送货单明细数据层实现类
    /// 作者：姚少鹏
    /// </summary>
    public class InvSendOrderDetailDaoImpl : IInvSendOrderDetailDao
    {
        FreedomERPEntities dbEntities = new FreedomERPEntities();

        /// <summary>
        /// 添加送货单明细
        /// </summary>
        /// <param name="sendOrderDetail"></param>
        public void addSendOrderDetail(InvSendOrderDetail sendOrderDetail)
        {
            dbEntities.InvSendOrderDetail.AddObject(sendOrderDetail);
            dbEntities.SaveChanges();
        }

        /// <summary>
        /// 删除送货单明细
        /// </summary>
        /// <param name="sendOrderDetail"></param>
        public void delSendOrderDetail(InvSendOrderDetail sendOrderDetail)
        {
            dbEntities.InvSendOrderDetail.DeleteObject(sendOrderDetail);
            dbEntities.SaveChanges();
        }

        /// <summary>
        /// 更新送货单明细
        /// </summary>
        /// <param name="sendOrderDetail"></param>
        public void updateSendOrderDetail(InvSendOrderDetail sendOrderDetail)
        {
            List<InvSendOrderDetail> sendOrderDetailList = (from ba in dbEntities.InvSendOrderDetail where ba.FId == sendOrderDetail.FId select ba).ToList<InvSendOrderDetail>();
            foreach (InvSendOrderDetail obj in sendOrderDetailList)
            {
                if (obj.FId.Equals(sendOrderDetail.FId))
                {
                    obj.FParentId = sendOrderDetail.FParentId;
                    obj.FProductId = sendOrderDetail.FProductId;
                    obj.FQty = sendOrderDetail.FQty;
                    obj.FNumber = sendOrderDetail.FNumber;
                    obj.FPrice = sendOrderDetail.FPrice;
                    obj.FAmount = sendOrderDetail.FAmount;
                    obj.FRemark = sendOrderDetail.FRemark;
                }
            }
            dbEntities.SaveChanges();
        }

        /// <summary>
        /// 根据FId获取送货单明细
        /// </summary>
        /// <param name="FId"></param>
        /// <returns></returns>
        public InvSendOrderDetail getSendOrderDetailByFId(string FId)
        {
            InvSendOrderDetail obj = null;

            List<InvSendOrderDetail> sendOrderDetailList = (from ba in dbEntities.InvSendOrderDetail where ba.FId == FId select ba).ToList<InvSendOrderDetail>();
            if (sendOrderDetailList != null && sendOrderDetailList.Count > 0)
                obj = sendOrderDetailList.First();

            return obj;
        }

        /// <summary>
        /// 根据FParentId获取送货单明细列表
        /// </summary>
        /// <param name="FParentId"></param>
        /// <returns></returns>
        public List<InvSendOrderDetail> getSendOrderDetailListByFParentId(string FParentId)
        {
            List<InvSendOrderDetail> sendOrderDetailList = (from ba in dbEntities.InvSendOrderDetail where ba.FParentId == FParentId select ba).ToList<InvSendOrderDetail>();

            return sendOrderDetailList;
        }

        /// <summary>
        /// 获取送货单明细列表
        /// </summary>
        /// <returns></returns>
        public List<InvSendOrderDetail> getSendOrderDetailList()
        {
            List<InvSendOrderDetail> sendOrderDetailList = dbEntities.InvSendOrderDetail.ToList<InvSendOrderDetail>();

            return sendOrderDetailList;
        }

        /// <summary>
        /// 根据条件获取明细列表
        /// 作者：梁建东
        /// </summary>
        public List<InvSendOrderDetail> GetSendOrderDetailListByFilter(Hashtable filters)
        {
            //where
            List<ObjectParameter> paramList = new List<ObjectParameter>();
            StringBuilder sbWhere = new StringBuilder();
            if (filters["FProductNumber"] != null && !filters["FProductNumber"].ToString().Equals(""))
            {
                sbWhere.Append("it.BdProduct.FNumber like @FProductNumber and ");
                ObjectParameter param = new ObjectParameter("FProductNumber", "%" + filters["FProductNumber"].ToString() + "%");
                paramList.Add(param);
            }
            if (filters["FProductName"] != null && !filters["FProductName"].ToString().Equals(""))
            {
                sbWhere.Append("it.BdProduct.FName like @FProductName and ");
                ObjectParameter param = new ObjectParameter("FProductName", "%" + filters["FProductName"].ToString() + "%");
                paramList.Add(param);
            }
            if (filters["FDateFrom"] != null && !filters["FDateFrom"].ToString().Equals(""))
            {
                sbWhere.Append("it.InvSendOrder.FBillDate between @FDateFrom and @FDateTo and ");
                ObjectParameter param = new ObjectParameter("FDateFrom", DateUtils.ParseStringToDateTime(filters["FDateFrom"].ToString()));
                paramList.Add(param);
                string FBillDateTo = "";
                if (filters["FDateTo"] != null && !filters["FDateTo"].ToString().Equals(""))
                {
                    FBillDateTo = filters["FDateTo"].ToString();
                }
                else
                {
                    FBillDateTo = DateUtils.GetCurDateFormat("yyyy/MM/dd HH:mm:ss");
                }
                ObjectParameter paramT = new ObjectParameter("FDateTo", DateUtils.ParseStringToDateTime(FBillDateTo));
                paramList.Add(paramT);
            }
            if (filters["FStatusId"] != null && !filters["FStatusId"].ToString().Equals(""))
            {
                sbWhere.Append("it.InvSendOrder.FStatusId=@FStatusId and ");
                ObjectParameter param = new ObjectParameter("FStatusId", filters["FStatusId"].ToString());
                paramList.Add(param);
            }

            sbWhere.Append("it.InvSendOrder.FOrgunitId=@orgunitParam and ");
            ObjectParameter paramOrg = new ObjectParameter("orgunitParam", filters["orgunitParam"].ToString());
            paramList.Add(paramOrg);

            sbWhere.Append("1=1");

            ObjectParameter[] objParameters = new ObjectParameter[paramList.Count];
            int c = 0;
            foreach (ObjectParameter paramTemp in paramList)
            {
                objParameters[c] = paramTemp;
                c++;
            }

            var query = dbEntities.InvSendOrderDetail.Where(sbWhere.ToString(), objParameters);

            List<InvSendOrderDetail> list = query.ToList();

            return list;
        }
    }
}